package me.smileface.http.proxy;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GdTest {
    static String url = "https://xy.amap.com/api/forward/aggregate?alsc-kbt-leads-center.LeadsQueryGatewayService.searchLeads__";

    // === 创建 OkHttpClient（自动支持 HTTP/2）===
    private static OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    private static int totalPage = 5106;
    private static int formPage = 100;

    public static void main(String[] args) throws Exception {

        for (int i = formPage; i <= totalPage; i++) {
            // === 构建请求体（Map）===
            Map<String, Object> param = new HashMap<>();
            param.put("pageSize", 10);
            param.put("page", i);
            param.put("sceneCode", "PC_PUBLIC_SALE_LEADS_SEARCH_V2");
            param.put("bizChannel", "EVE_PC");
            param.put("type", "ITEM_SALE");
            param.put("deviceInfo", "{\"platform\":\"pc\"}");
            param.put("status", "INIT");

            Map<String, Object> extraHeader = new HashMap<>();
            extraHeader.put("requestid", "baacdcf8-27fe-47c5-97b3-f3dafc9ec315");
            extraHeader.put("requestsource", 7);

            Map<String, Object> request = new HashMap<>();
            request.put("action", "alsc-kbt-leads-center.LeadsQueryGatewayService.searchLeads");
            request.put("param", param);
            request.put("requestType", "msePc");
            request.put("extraHeader", extraHeader);
            request.put("skipCompatGwApi", false);

            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("requests", Arrays.asList(request));

            String jsonBody = JSON.toJSONString(requestBody);

            // === 构建 Cookie ===
            String cookie = "EGG_SESS=m_49SgzCD74gmQgJuSxeJTwCM8MWB6CFTqvESDpHuZc6b6iQmoX5QJUJ67QbXo5NQ_KgSmkzp39H3KvhY9Fcdw==; " +
                    "xlly_s=1; " +
                    "cna=PVt+IZTloiwCAd6s9GxGpKu8; " +
                    "_uab_collina=176104582252360535845416; " +
                    "passport_login=MTkyNjI5MDEzNyx4eTEzODg4NjI0MjEwLGRpc19kcDVocWZ6aWluMjV6cmx6Mmtsb2VodnNweG9ucDVvYywxNzYxMDQ1ODUzLFpXWm1OemN6TW1JMlptUXhOelUyTW1FMk1qSmtaREkxTXpjNE9UTXlORE09; " +
                    "oauth_state=b03d8791329de39751740e79deda4a75; " +
                    "amap-xy-token=pcQTIyRDRFQ0FENDcxMTM1QTZGNTU4QUZWQ4YmFiYTItYTRhNC00ZDIxLThhZGQt1vDovI; " +
                    "x-gray-bucket=PT5; " +
                    "isg=BExMBZ27SGjTNVyUO8DEFFyyHap-hfAvINcMt6YP6feaMe07zpbbvtDH0TkJfiiH; " +
                    "tfstk=gLTx4VZRpYDDMBw4MdmojooicWhu60AVNKRQSOX0fLpJgIIsgIm213p6UiXfnmlTWTA6otagmUh9KdLgoI_GBLpypj8GnZO_5dpWiEY9_PC61dlVIjokgI7N5vDHW2A2gvXaHCTYl4aWa66110jjp4G6pvDn-4AD3FPjKd4lmbI5_T1f1t1swz1NGN1fGOGRF1CUfr915bhR16C_Ct1jPa1d_N66CNGJNT5O5s915bd5U1GyIAB7GO490EkjWbm0mPU9ysIABPX8WCTb-i6QataTWTfADoAfePa6PhekkeBjY7XNotRfMduUuZs9fEbWlYg1RHJJWM_KjjSX63pNoUG_c9tDnGYc2-Zf9ZBAAEsoJyORf3LRoEMaYQARhMQkq0kcjZpvY9Sj4YJ9wtv6kgwxEOxHZEsJdY4JQGK6oaTjF2IP8eYKgEehp1qjwbEabi1zUoGL6BdW5G1RKj3TblSraPqvSKrabgflw9cxBlrN4FC..";


            // === 构建 Request ===
            MediaType MEDIA_TYPE = MediaType.get("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(MEDIA_TYPE, jsonBody);

            Request requestObj = new Request.Builder()
                    .url(url)
                    .post(body)
                    .addHeader("Accept", "application/json")
                    .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                    .addHeader("Bx-V", "2.5.31")
                    .addHeader("Content-Type", "application/json;charset=UTF-8")
                    .addHeader("Origin", "https://xy.amap.com")
                    .addHeader("Referer", "https://xy.amap.com/sale-pc/xy-leads-pc/leads/public")
                    .addHeader("Requestid", "baacdcf8-27fe-47c5-97b3-f3dafc9ec313")
                    .addHeader("Requestsource", "7")
                    .addHeader("Sec-Ch-Ua", "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"")
                    .addHeader("Sec-Ch-Ua-Mobile", "?0")
                    .addHeader("Sec-Ch-Ua-Platform", "\"Windows\"")
                    .addHeader("Sec-Fetch-Dest", "empty")
                    .addHeader("Sec-Fetch-Mode", "cors")
                    .addHeader("Sec-Fetch-Site", "same-origin")
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
                    .addHeader("Cookie", cookie)
                    .build();

            // === 发送请求 ===
            try (Response response = client.newCall(requestObj).execute()) {
                String responseBody = response.body().string();
                System.out.println("页码：" + i + " Status: " + response.code() + " - " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " len:" + responseBody.length());


                FileUtil.writeString(responseBody, "D:\\test\\gd\\" + i + ".json", "utf-8");
            }

            int sleep = RandomUtil.randomInt(5000, 9000);
            ThreadUtil.safeSleep(sleep);
        }

    }
}